package com.domain.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.domain.model.Dict;
import com.domain.service.DictService;

@Controller
@RequestMapping("/dict")
public class DictController extends SuperController {
	@Resource
	private DictService dictService;
	
	
	/**
	 * 查询字典列表
	 */
	@RequestMapping("/dictList.html")
	public String dictList(HttpServletRequest request,String porCode,ModelMap model){
		List<Map<String, Object>> list =dictService.dictList();
		request.setAttribute("list", list);
		model.put(POR_CODE, porCode);
		return "system/dictList";
	}
	
	
	/**
	 * 进入字典编辑页面
	 */
	@RequestMapping("/editDict/{porCode}.html")
	public String toEditUser(HttpServletRequest request,Dict dict,@PathVariable String porCode,ModelMap model){
		if (null != dict && null !=dict.getId()) {
			dict =dictService.getDictById(dict.getId());
			model.put("entity", dict);
		}
		model.put(POR_CODE, porCode);
		return "system/editDict";
	}

	/**
	 * 编辑用户信息角色
	 */
	@RequestMapping("/save/{porCode}.html")
	public String doEditDict(HttpServletRequest request,Dict dict,@PathVariable String porCode,ModelMap model){
		try {
			if (dict.getId()==null) {
				dict.setUser_id(getCurrentUser(request).getId());
				dict.setCreate_date(new Date());
				dictService.add(dict);
			}else {
				dictService.edit(dict);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.put(POR_CODE, porCode);
		return "redirect:/dict/dictList.html";
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete/{porCode}.html")
	public String delete(Long id,@PathVariable String porCode,ModelMap model){
		try {
			dictService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.put(POR_CODE, porCode);
		return "redirect:/dict/dictList.html";
	}
	
	
}
